package ch01.sec07;

public class SwitchDemo {
    public static void demo(int seasonCode) {
        System.out.printf("seasonCode = %d%n", seasonCode);

        // switch expression
        String seasonName = switch (seasonCode) {
            case 0 -> "Spring";
            case 1 -> "Summer";
            case 2 -> "Fall";
            case 3 -> "Winter";
            default -> {
                System.out.println("???");
                yield "";
            }
        };
        System.out.println(seasonName);

        // switch statement
        switch (seasonCode) { // switch statement
            case 0 -> seasonName = "Spring";
            case 1 -> seasonName = "Summer";
            case 2 -> seasonName = "Fall";
            case 3 -> seasonName = "Winter";
            default -> {
                System.out.println("???");
                seasonName = "";
            }
        }
        System.out.println(seasonName);

        // Multiple labels
        int numLetters = switch (seasonName) {
            case "Spring", "Summer", "Winter" -> 6;
            case "Fall" -> 4;
            default -> throw new IllegalArgumentException();
        };
        System.out.println(numLetters);

        // switch expression with fall-through
        numLetters = switch (seasonName) {
            case "Spring":
                System.out.println("spring time!");
            case "AAA":
                System.out.println("AAA");
            case "BBB":
                System.out.println("BBB");
            case "Summer", "Winter":
                yield 1;
            case "Fall":
                yield 2;
            default:
                yield -1;
//                throw new IllegalArgumentException();
        };
        System.out.println(numLetters);

        // switch statement with fall-through
        switch (seasonName) {
            case "Spring":
                System.out.println("spring time!");
            case "Summer", "Winter":
                numLetters = 3;
                break;
            case "Fall":
                numLetters = 4;
                break;
            default:
                numLetters = -2;
//                throw new IllegalArgumentException();
        }
        System.out.println(numLetters);
    }

    public static void switchDemo(int age) {
        // 其中%d是整数占位符，%n是换行符
        System.out.printf("age = %d%n", age);
        switch (age) {
            case 20:
                System.out.println("上学");
            case 24:
                System.out.println("苏州工作");
                break;
            case 30:
                System.out.println("洛阳工作");
                break;
            default:
                System.out.println("未知");
                break; // 可省略
        }
    }

    public static void main(String[] args) {
        switchDemo(20);
        System.out.println("====================");
        demo(0);
//        demo(1);
//        demo(2);
//        demo(3);
//        demo(4); // Will throw an exception

    }
}
